package org.eenie.kdassistant.ui.setting;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.preference.EditTextPreference;
import android.support.v7.preference.ListPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;

import org.eenie.kdassistant.App;
import org.eenie.kdassistant.R;
import org.eenie.kdassistant.net.ParamsAnalog;
import org.eenie.kdassistant.net.yd_api.entity.UserEntity;
import org.eenie.kdassistant.net.yd_api.entity.UserEntityDao;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class SettingFragment extends PreferenceFragmentCompat implements Preference.OnPreferenceChangeListener {

    EditTextPreference xz_uuid;
    EditTextPreference zzt_token;

    EditTextPreference zzt_openid;
    EditTextPreference zzt_empCode;
    EditTextPreference kdy_sessionid;
    EditTextPreference device_imei;
    EditTextPreference device_location;


    ListPreference yd_user;

    ParamsAnalog mParamsAnalog;

    UserEntityDao mUserEntityDao;


    @Override
    public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
        addPreferencesFromResource(R.xml.setting_preference);
        mUserEntityDao = ((SettingActivity) getActivity()).getApplicationComponent().getDaoSession().getUserEntityDao();

        mParamsAnalog = ((App) getActivity().getApplication()).getApplicationComponent().getParamsAnalog();

        xz_uuid = (EditTextPreference) findPreference("xz_uuid");
        zzt_token = (EditTextPreference) findPreference("zzt_token");
        zzt_openid = (EditTextPreference) findPreference("zzt_openid");
        kdy_sessionid = (EditTextPreference) findPreference("kdy_sessionid");
        device_imei = (EditTextPreference) findPreference("device_imei");
        device_location = (EditTextPreference) findPreference("device_location");
        zzt_empCode = (EditTextPreference) findPreference("zzt_empCode");
        yd_user = (ListPreference) findPreference("yd_user");




//        device_location.setSummary(mParamsAnalog.getXz_uuid());
//        device_location.setText(mParamsAnalog.getXz_uuid());



        xz_uuid.setOnPreferenceChangeListener(this);
        zzt_token.setOnPreferenceChangeListener(this);
        zzt_openid.setOnPreferenceChangeListener(this);
        kdy_sessionid.setOnPreferenceChangeListener(this);
        device_imei.setOnPreferenceChangeListener(this);
        device_location.setOnPreferenceChangeListener(this);
        zzt_empCode.setOnPreferenceChangeListener(this);
        yd_user.setOnPreferenceChangeListener(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        init();
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if (preference == xz_uuid) {
            mParamsAnalog.setXz_uuid(newValue.toString());
        } else if (preference == zzt_token) {
            mParamsAnalog.setZzt_token(newValue.toString());
        } else if (preference == zzt_openid) {
            mParamsAnalog.setZzt_opendid(newValue.toString());
        } else if (preference == kdy_sessionid) {
            mParamsAnalog.setKdy_sessionId(newValue.toString());
        } else if (preference == device_imei) {
            mParamsAnalog.setDevice_imei(newValue.toString());
        } else if (preference == zzt_empCode) {
            mParamsAnalog.setZzt_empCode(newValue.toString());
        } else if (preference == yd_user) {



            UserEntity oldEn = mUserEntityDao.queryBuilder().where(UserEntityDao.Properties.Id.eq(((ListPreference) preference).getValue())).build().unique();
            UserEntity newEn = mUserEntityDao.queryBuilder().where(UserEntityDao.Properties.Id.eq(newValue)).build().unique();
            yd_user.setSummary(newEn.getName());

            oldEn.setChecked(false);
            newEn.setChecked(true);
            mUserEntityDao.update(oldEn);
            mUserEntityDao.update(newEn);
            return true;
        }


        reload((EditTextPreference) preference, newValue.toString());
        return true;
    }


    private void reload(EditTextPreference preference, String value) {
        preference.setSummary(value);
        preference.setText(value);
    }


    private void init() {

        xz_uuid.setSummary(mParamsAnalog.getXz_uuid());
        xz_uuid.setText(mParamsAnalog.getXz_uuid());

        zzt_token.setSummary(mParamsAnalog.getZzt_token());
        zzt_token.setText(mParamsAnalog.getZzt_token());

        zzt_openid.setSummary(mParamsAnalog.getZzt_opendid());
        zzt_openid.setText(mParamsAnalog.getZzt_opendid());

        zzt_empCode.setSummary(mParamsAnalog.getZzt_empCode());
        zzt_empCode.setText(mParamsAnalog.getZzt_empCode());

        kdy_sessionid.setSummary(mParamsAnalog.getKdy_sessionId());
        kdy_sessionid.setText(mParamsAnalog.getKdy_sessionId());

        device_imei.setSummary(mParamsAnalog.getDevice_imei());
        device_imei.setText(mParamsAnalog.getDevice_imei());


        device_location.setText(String.format("%s,%s", mParamsAnalog.getDevice_lat(), mParamsAnalog.getDevice_lng()));

        List<UserEntity> userEntities = mUserEntityDao.loadAll();

        String[] names = new String[userEntities.size()];
        String[] values = new String[userEntities.size()];

        for (int i = 0; i < userEntities.size(); i++) {
            names[i] = userEntities.get(i).getName();
            values[i] = userEntities.get(i).getId();
        }


        yd_user.setEntries(names);
        yd_user.setEntryValues(values);

        UserEntity entity = mUserEntityDao.queryBuilder().where(UserEntityDao.Properties.Checked.eq(true)).unique();


        if (entity != null) {
            yd_user.setSummary(entity.getName());
            yd_user.setValue(entity.getId());
        }
    }

}
